package com.ruoyi.basedata.controller;

import com.ruoyi.basedata.domain.GoodUnit;
import com.ruoyi.basedata.service.IGoodUnitService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品单位Controller
 *
 * @author lmj
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/unit")
@Slf4j
public class GoodUnitController extends BaseController {
    @Autowired
    private IGoodUnitService goodUnitService;

    /**
     * 查询商品单位列表
     */
    @RequiresPermissions("basedata:unit:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodUnit goodUnit) {
        startPage();
        List<GoodUnit> list = goodUnitService.selectGoodUnitList(goodUnit);
        return getDataTable(list);
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 获取所有商品单位
     * @date 2024/11/9 上午8:36
     **/
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        return success(goodUnitService.selectGoodUnitList(new GoodUnit()));
    }

    /**
     * 导出商品单位列表
     */
    @RequiresPermissions("basedata:unit:export")
    @Log(title = "商品单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodUnit goodUnit) {
        List<GoodUnit> list = goodUnitService.selectGoodUnitList(goodUnit);
        ExcelUtil<GoodUnit> util = new ExcelUtil<GoodUnit>(GoodUnit.class);
        util.exportExcel(response, list, "商品单位数据");
    }

    /**
     * 获取商品单位详细信息
     */
    @RequiresPermissions("basedata:unit:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(goodUnitService.selectGoodUnitById(id));
    }

    /**
     * 新增商品单位
     */
    @RequiresPermissions("basedata:unit:add")
    @Log(title = "商品单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodUnit goodUnit) {
        log.info("GoodUnitController add param :{}", goodUnit);
        AjaxResult msg = goodUnitService.insertGoodUnit(goodUnit);
        return msg;
    }

    /**
     * 修改商品单位
     */
    @RequiresPermissions("basedata:unit:edit")
    @Log(title = "商品单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodUnit goodUnit) {
        log.info("GoodUnitController edit param :{}", goodUnit);
        AjaxResult msg = goodUnitService.updateGoodUnit(goodUnit);
        return msg;
    }

    /**
     * 删除商品单位
     */
    @RequiresPermissions("basedata:unit:remove")
    @Log(title = "商品单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return goodUnitService.deleteGoodUnitByIds(ids);
    }
}
